<?php

namespace App\Http\Controllers;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;

use Illuminate\Support\Facades\Auth;
use App\Models\Users;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;

class BaseController extends Controller
{


    protected $user;
    public function __construct()
    {

        $this->middleware('jwt.custom.auth', ['except' => ['login','refresh']]);

        $this->user =Auth::guard('web')->user();

    }

//    public function success($code,$data = []): \Illuminate\Http\JsonResponse
//    {
//        return response()->json([
//            'status' => true,
//            'code'  => 200,
//            'message' => config('errorCode.code')[(int) $code],
//            'data'  => $data,
//            'success_time'  => date("Y-m-d H:i:s")
//        ]);
//    }
//
//    public function fail($code, $data = []): \Illuminate\Http\JsonResponse
//    {
//        return response()->json([
//            'status' => false,
//            'code'  => $code,
//            'message' => config('errorCode.code')[(int) $code],
//            'data'  => $data,
//            'failed_time'  => date("Y-m-d H:i:s")
//        ]);
//    }

    protected function getUserPermissionAndMenu(): array
    {
       if(empty($this->user->roles->toArray())){
           return ["permission"=>[],"menu"=>[]];
       }

           $user_role_per = $this->user->roles[0]->with(["permissions"=>function($query){
               $query->select('permission.id as per_id','parentid','name','api_route','web_route','method','no','is_need_auth','is_system_menu','status');
               $query->where("status",1);
               $query->where("is_need_auth",1);
           }])->get()->toArray();

           $permissions = [];
           $menu = [];
           foreach ($user_role_per as $vo){
               if(!empty($vo['permissions'])){
                   foreach ($vo['permissions'] as $v){
                       $permissions[$v['per_id']] = $v;
                       if($v['is_system_menu']==1){
                           $menu[$v['per_id']] = $v;
                       }
                   }
               }
           }
           $token = JWTAuth::getToken(); //可以换成接受到的token,  JWTAuth::setToken($token)
           $payload = JWTAuth::manager()->getJWTProvider()->decode($token->get());
           $user_per = json_decode(json_encode($payload['extra_permission'], true), true);
           foreach ($user_per as $key=>$vo){
               $vo['pivot']['is_extra'] = 1;
               $permissions[$key] = $vo;
           }



        return ["permission"=>$permissions,"menu"=>$menu];
    }

    protected function getUserRoles(): array
    {

        $userInfo = $this->user->with(['roles'=>function($query){
            $query->select("name as role_name");
            $query->where("status",1);
        }])->first()->toArray();

        return Arr::pluck($userInfo['roles'], 'role_name');


    }

    protected  function getEloquentSqlWithBindings($query): string
    {
        return vsprintf(str_replace('?', '%s', $query->toSql()), collect($query->getBindings())->map(function ($binding) {
            return is_numeric($binding) ? $binding : "'{$binding}'";
        })->toArray());
    }

}
